from flask import Blueprint, render_template, url_for, redirect, flash, request, send_from_directory
from personal_blog.models import Admin, Post, Category
from flask_login import login_user, logout_user, login_required, current_user
from personal_blog.form import LoginForm, RegisterForm, PostForm, AdminForm, SetPasswordform, CategoryForm
from personal_blog.setting import redirect_back
from personal_blog.extensions import db
import personal_blog.tools as tools
from flask_ckeditor import CKEditor, CKEditorField, upload_fail, upload_success
# import personal_blog.config as config
import os
admin_bp = Blueprint('admin', __name__)


@admin_bp.route('/register', methods=['GET', 'POST'])
def register():
    form = RegisterForm()
    print(request.remote_addr)
    if form.validate_on_submit():
        username = form.username.data
        password = form.password.data
        # remember = form.remember.data

        admin = Admin(
            username=username,
        )
        # print(password)
        admin.set_password(password)

        db.session.add(admin)
        db.session.commit()
        flash('注册成功', 'info')
        return redirect(url_for('admin.login'))
        # admin = Admin.query.first()
        # if admin:
        # if admin.check_password(password):
        # login_user(admin, remember)
        #

    return render_template('admin/register.html', form=form)

@admin_bp.route('/login', methods=['GET', 'POST'])
def login():
    if current_user.is_authenticated:
        flash(u'你已经登录了！', 'info')
        return redirect(url_for('blog.index'))

    form = LoginForm()
    if form.validate_on_submit():
        username = form.username.data
        password = form.password.data
        remember = form.remember.data

        admin = Admin.query.filter_by(username=username).first()
        print('user',admin.id)
        # config.get_id = admin.id
        # config._init()
        with open('personal_blog/config','w') as f:
            f.write(str(admin.id))
        # config.set_value(admin.id)
        # print(config.get_value())
        if admin:
            if admin.check_password(password):
                login_user(admin, remember)
                # config.user_id =admin.get_id()
                # print(config.user_id)
                flash('登录成功', 'info')
                tools.add_log(request.remote_addr,'登录')
                return redirect_back()

            flash('账号密码错误', 'warning')
        else:
            flash('No account.', 'warning')
    return render_template('admin/login.html', form=form)


@admin_bp.route('/logout')
@login_required
def logout():
    logout_user()
    flash(u'退出登录', 'info')
    return redirect_back()


@admin_bp.route('/setting')
@login_required  # 视图保护，只有登录了才能看到这个被这个装饰器修饰的路由
def setting():
    return render_template('admin/setting.html')


@admin_bp.route('/category/manage')
@login_required
def manage_category():
    categories = Category.query.all()
    return render_template('admin/manage_category.html', categories=categories)


@admin_bp.route('/category/new', methods=['GET', 'POST'])
@login_required
def new_category():
    form = CategoryForm()
    if form.validate_on_submit():
        name = form.name.data
        category = Category(name=name)
        db.session.add(category)
        db.session.commit()
        flash('分类创建成功！', 'success')
        tools.add_log(request.remote_addr, '新建分类')
        return redirect(url_for('.manage_category'))
    return render_template('admin/new_category.html', form=form)


@admin_bp.route('/category/<int:category_id>/edit', methods=['GET', 'POST'])
@login_required
def edit_category(category_id):
    form = CategoryForm()
    category = Category.query.get_or_404(category_id)
    if category.id == 1:
        flash('你不能修改默认的分类', 'warning')
        return redirect('blog.index')
    if form.validate_on_submit():
        category.name = form.name.data
        db.session.commit()
        flash('文章分类更新成功')
        tools.add_log(request.remote_addr, '编辑分类')
        return redirect(url_for('.manage_category'))
    form.name.data = category.name
    return render_template('/admin/edit_category.html', form=form)


@admin_bp.route('/category<int:category_id>/delete', methods=['POST'])
@login_required
def delete_category(category_id):
    category = Category.query.get_or_404(category_id)
    if category.id == 1:
        flash('你不能删除默认的分类', 'warning')
        return redirect('blog.index')
    category.delete()
    flash('分类删除成功')
    tools.add_log(request.remote_addr, '删除分类')
    return redirect(url_for('.manage_category'))


@admin_bp.route('/manage_post')  # 文章管理
@login_required
def manage():
    with open('personal_blog/config', 'r') as f:
        s = f.read()
    id = int(s)
    print(id)
    page = request.args.get('page', 1, type=int)  # 从查询字符串中获取当前页数
    per_page = 5  # 每页的数量
    pagination = Post.query.filter_by(user_id=id).order_by(Post.timestamp.desc()).paginate(page, per_page=per_page)  # 分页对象
    posts = pagination.items  # 当前页数记录的列表

    return render_template('admin/manage_post.html', pagination=pagination, posts=posts)


@admin_bp.route('/post/<int:post_id>/edit', methods=['GET', 'POST'])
@login_required
def edit_post(post_id):
    form = PostForm()
    post = Post.query.get_or_404(post_id)
    if form.validate_on_submit():
        post.title = form.title.data
        post.category = Category.query.get(form.category.data)
        post.body = form.body.data
        db.session.commit()
        flash('文章更新成功')
        tools.add_log(request.remote_addr, '编辑文章')
        return redirect(url_for('blog.show_post', post_id=post.id))
    form.title.data = post.title
    form.category.data = post.category_id
    form.body.data = post.body
    return render_template('/admin/edit_post.html', form=form)


@admin_bp.route('/post/<int:post_id>/delete', methods=['POST'])
@login_required
def delete_post(post_id):
    post = Post.query.get_or_404(post_id)
    db.session.delete(post)
    db.session.commit()
    tools.add_log(request.remote_addr, '删除文章')
    flash('文章删除成功！')
    return redirect_back()


@admin_bp.route('/new_post', methods=['POST', 'GET'])
@login_required
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        title = form.title.data
        body = form.body.data
        category = Category.query.get(form.category.data)
        with open('personal_blog/config', 'r') as f:
            s = f.read()
        id = int(s)
        print(id)
        admin = Admin.query.get(id)
        post = Post(title=title, category=category, body=body,admin=admin)
        db.session.add(post)
        db.session.commit()
        tools.add_log(request.remote_addr, '新建文章')
        flash('博客发表成功')
        return redirect(url_for('blog.index'))
    return render_template('/admin/new_post.html', form=form)


# 修改关于
@admin_bp.route('/edit_individual_resume', methods=['GET', 'POST'])
def edit_indicidual_resume():
    cate = Category.query.filter_by(name='individual_resume').first()
    post = Post.query.filter_by(category=cate).first()
    return redirect(url_for('admin.edit_post', post_id=post.id))


# 修改网站介绍
@admin_bp.route('/edit_this_site', methods=['GET', 'POST'])
def edit_this_site():
    cate = Category.query.filter_by(name='this_site').first()
    post = Post.query.filter_by(category=cate).first()
    return redirect(url_for('admin.edit_post', post_id=post.id))


@admin_bp.route('/set_password', methods=['GET', 'POST'])
@login_required
def set_password():
    form = SetPasswordform()
    if form.validate_on_submit():
        username = form.username.data
        print(username)
        admin = Admin.query.filter_by(username=username).first()
        # admin = Admin.query.first()
        new_password = form.password.data
        admin.username = form.username.data
        admin.set_password(new_password)
        db.session.add(admin)
        db.session.commit()
        flash('密码修改成功')
        return redirect(url_for('blog.index'))
    return render_template('/admin/set_password.html', form=form)


@admin_bp.route('/files/<path:filename>')
def uploaded_files(filename):
    path = '/the/uploaded/directory'
    return send_from_directory(path, filename)


@admin_bp.route('/upload', methods=['POST'])
@login_required
def upload():
    f = request.files.get('upload')  # 获取上传图片文件对象, 键必须为'upload'
    extensions = f.filename.split('.')[-1].lower()
    if extensions not in ['png', 'jpg', 'gif', 'jpeg']:
        return uploaded_fail(message='只能上传图片')
    f.save(os.path.join(app.config['UPLOAED'], f.filename))
    url = url_for('uploads_files', filename=f.filename)
    return upload_success(url=url)
